/**
 * 退出登录  ok
 */
function doLoginOut() {
    var v = confirm("确认退出登录？");
    if (v) {
        alert("已经成功退出！！！");
        window.location.href = 'http://localhost:8080/management';
    }
}



// ***************  user逻辑  ***************   
/**
 * 显示所有员工 -> ShowAllMember.jsp  ok
 */
function showAllMember() {
    $.ajax({
        dataType: 'json',
        contentType: '/application/json;charset=UTF-8',
        async: true,
        type: "GET",
        url: 'person/showAllMember.do',
        success: function(d) {
			// 模板导入
			$("#mainframe").load("jsp/ShowAllMember.jsp", function(){
				//callback
				var alluser = d.allUsers;
				for(var i = 0;i< alluser.length;i++){
					var user = alluser[i];
					var data = "<tr><td>"+ user.userid + "</td>"+
						"<td>"+user.name+"</td>"+
						"<td>"+user.username+"</td>"+
						"<td>"+user.phone+"</td>"+
						"<td>"+user.email+"</td>"+
						"<td>"+user.department+"</td>"+
						"<td>"+user.company+"</td>"+
						"<td>"+user.roleid+"</td>"+
						"<td><button class=\"edit\" onclick=\"showEditUser(" + user.userid + ")\">编辑</button>"+
						"<button class=\"delete\" onclick=\"deleteUser(" + user.userid + ")\">删除</button></td></tr>";
					$("#allUsers").append(data);
				}
			});
        },
        error: function() {
            alert("未知错误？");
        }
    });
}



/**
 * 显示addnewuser -> AddNewMember.jsp   ok
 */
function showAddUser() {
	$("#mainframe").load("jsp/AddNewMember.jsp");
}


/**
 * 显示edituser -> EditMember.jsp   ok
 */
function showEditUser(uid) {
	$.ajax({
        dataType: 'json',
        contentType: 'application/json',
        async: true,
        type: "POST",
        data: JSON.stringify({"userid": uid}),
		url: "person/toEditMember.do",
        success: function(d) {
			$("#mainframe").load("jsp/EditMember.jsp");
        },
        error: function() {
            alert("未知错误？");
        }
    });
}


/**
 * 员工信息更新提交    ok
 */
function updateUser(userid, EDroleid) {  
    if (EDroleid == "1") {
        var name = document.getElementById("name").value;
        var email = document.getElementById("email").value;
        var phone = document.getElementById("phone").value;
        var roleid = document.getElementById("roleid").value;
        var data = JSON.stringify({"name":name, "email": email, "phone": phone , "userid":userid,"roleid": roleid});
    } else {
        var name = document.getElementById("name").value;
        var email = document.getElementById("email").value;
        var phone = document.getElementById("phone").value;
        var data = JSON.stringify({"name":name, "email": email, "phone": phone, "userid":userid });
    }
    $.ajax({
    	dataType: 'json',
        contentType: 'application/json',
        async: true,
        type: "POST",
        data: data,
		url: "person/upadteMember.do",
		success: function(d) {
			alert(d.msg);
        },
        error: function() {
            alert("未知错误？");
        }
    });

}

/**
 * 删除员工  ok
 */
function deleteUser(userId) {  
    var v = confirm("确认删除用户？");
    if (v) {
        $.ajax({
            dataType: 'json',
	        contentType: 'application/json',
	        async: true,
	        type: "POST",
	        data: JSON.stringify({"userid": userId}),
			url: "person/delMember.do",
			success: function(d) {
				alert(d.msg);
	        },
	        error: function() {
	            alert("未知错误？");
	        }
        });
    }
}


/**
 * 添加员工     ok
 */
function addNewUser() { 
    // var userid = document.getElementById("userid").value;
    var name = document.getElementById("name").value;
    var username = document.getElementById("username").value;
    var email = document.getElementById("email").value;
    var phone = document.getElementById("phone").value;
    var department = document.getElementById("department").value;
    var roleid = document.getElementById("roleid").value;
    var company = document.getElementById("company").value;

    var mydata = JSON.stringify({"username":username,"name":name,"email":email,"phone":phone,
								 "company":company,"roleid":roleid,"department":department});
    $.ajax({
        dataType: 'json',
	    contentType: 'application/json',
	    async: true,
	    type: "POST",
	    data: mydata,
		url: "person/addMember.do",
		success: function(d) {
			alert(d.msg);
	    },
	    error: function() {
	        alert("未知错误？");
	    }
        
    });
}


// ***************  project逻辑  ***************   2021.6
/**
 * 显示所有项目 -> ShowAllPro.jsp    ok
 */
function showAllPro() {
    $.ajax({
        dataType: 'json',
        contentType: '/application/json;charset=UTF-8',
        async: true,
        type: "GET",
        url: 'proj/showAllProj.do',
        success: function(d) {
			// 模板导入
			$("#mainframe").load("jsp/ShowAllPro.jsp", function(){
				// callback
				var allPro = d.allPro;
				for(var i = 0;i< allPro.length;i++){
					var proj = allPro[i];
					var data = "<tr><td>"+pro.proid+"</td>"+
						"<td>"+pro.proname+"</td>"+
						"<td>"+pro.begindate+"</td>"+
						"<td>"+pro.enddate+"</td>"+
						"<td>"+pro.remark+"</td>"+
						"<td>"+pro.depid+"</td>"+
						"<td>"+pro.isdelete+"</td>"+
						"<td><button class=\"edit\" onclick=\"editPro("+pro.proid+")\">编辑</button>"+
						"<button class=\"delete\" onclick=\"deletePro("+pro.proid+", 1)\">废弃</button></td></tr>";
					$("#allProjs").append(data);
				}
			});
        },
        error: function() {
            alert("未知错误？");
        }
    });
}

/**
 * 删除项目  
 */
function deletePro(proId, m) {
    var v = confirm("确认废弃项目？");
    if (v) {
        $.ajax({
            type: "GET",
            url: 'http://localhost:8080/management/ProjectServlet',
            data: 'action=3&proId=' + proId + '&rad=' + Math.random() + '&m=' + m,
            success: function(msg) {
                $("#mainframe").html(msg);
            }
        });
    }
}

/**
 * 显示添加项目    ok
 */
function showAddPro() {
    // 模板导入
	$("#mainframe").load("jsp/AddNewPro.jsp");
}

/**
 * 添加项目  半成品
 */
function addNewPro(userid, roleid) {
    // var proId = document.getElementById("proid").value;
    var proName = document.getElementById("proname").value;
    var beginDate = document.getElementById("begindate").value;
    var endDate = document.getElementById("enddate").value;
    // var state = document.getElementById("state").value;
    var remark = document.getElementById("remark").value;
    var belong = document.getElementById("belong").value;


    var mydata = JSON.stringify({"proname":proName, "begindate":beginDate, "enddate":endDate,
    							 "remark":remark, "belong":belong, "userid":userid, "roleid":roleid});

    $.ajax({
		dataType: 'json',
        contentType: 'application/json',
        async: true,
        type: "POST",
        url: 'proj/showAllProj.do',
        data: mydata,
        success: function(msg) {
            $("#updateresult4").html(msg.msg);
        }
    });

}

/**
 * 返回编辑页面 -> EditPro.jsp  
 */
function editPro(proid) {
    $.ajax({
        type: "GET",
        url: 'http://localhost:8080/management/ProjectServlet',
        data: 'action=4&proid=' + proid + '&rad=' + Math.random(),
        success: function(msg) {
            $("#mainframe").html(msg);
        }
    });
}

/**
 * 更新Pro  
 */
function updatePro(proId) {
    // var proId = document.getElementById("proid").value;
    var proName = document.getElementById("proname").value;
    var beginDate = document.getElementById("begindate").value;
    var endDate = document.getElementById("enddate").value;
    var customer = document.getElementById("customer").value;
    // var state = document.getElementById("state").value;
    var remark = document.getElementById("remark").value;
    var belong = document.getElementById("belong").value;

    var mydata = 'action=2' + '&proname=' + proName + '&begindate=' +
        beginDate + '&enddate=' + endDate + '&customer=' + customer +
        '&remark=' + remark + '&belong=' + belong + '&proId=' + proId;
    $.ajax({
        type: "GET",
        url: 'http://localhost:8080/management/ProjectServlet',
        data: mydata,
        success: function(msg) {
            $("#updateresult3").html(msg);
        }
    });
}



// ***************  分角色登录逻辑  ***************   2021.6.13
/**
 * 打开我的项目(员工)  
 */
function openPro(userid, p) {
    $.ajax({
        type: "GET",
        url: 'http://localhost:8080/management/ProjectServlet',
        data: '&action=7' + '&userid=' + userid + "&p=" + p + '&rad=' + Math.random(),
        success: function(msg) {
            $("#mainframe").html(msg);
        }
    });
}

/**
 * 项目人员查询  
 */
function proInfo(proid) {
    $.ajax({
        type: "GET",
        url: 'http://localhost:8080/management/ProjectServlet',
        data: '&action=8' + '&proid=' + proid + '&rad=' + Math.random(),
        success: function(msg) {
            $("#mainframe").html(msg);
        }
    });
}

/**
 * 转到重置密码    ok
 */
function toReset() {
    $("#mainframe").load("jsp/resetPwd.jsp");
}

/**
 * 搜索人员(管理员用来重置密码)     ok
 */
function searchUser() {
    var userReset = document.getElementById("userReset").value;
    $.ajax({
        dataType: 'json',
        contentType: 'application/json',
        async: true,
        type: "POST",
        data: JSON.stringify({"userid":parseInt(userReset)}),
        url: 'person/searchUser.do',
        success: function(d) {
			// 模板导入
			$("#mainframe").load("jsp/onePersonInfo.jsp", function(){
				// callback
				var searchRes = d.searchRes;
				var data = "<td>"+searchRes.userid+"</td>"+
							"<td>"+searchRes.name+"</td>"+
							"<td>"+searchRes.username+"</td>"+
							"<td>"+searchRes.phone+"</td>"+
							"<td>"+searchRes.email+"</td>"+
							"<td>"+searchRes.department+"</td>"+
							"<td>"+searchRes.company+"</td>"+
							"<td>"+searchRes.roleid+"</td>"+
							"<td><button class=\"edit\" onclick=\"resetPwd("+searchRes.userid+")\">重置密码</button>"+"</td>";
				$("#searchRes").append(data);	
			});
        },
        error: function() {
            alert("未知错误？");
        }
    });
}

/**
 * 重置密码（管理员）  ok
 */
function resetPwd(userReset) {
    $.ajax({
        dataType: 'json',
        contentType: 'application/json',
        async: true,
        type: "POST",
        data: JSON.stringify({"userid":parseInt(userReset)}),
        url: 'person/resetPwd.do',
        success: function(d) {
			alert(d.msg);
        },
        error: function() {
            alert("未知错误？");
        }
    });
}


// ***************  留言模块 和 下发任务 ***************   2021.6.26
/**
 * 展示留言   ok
 */
function showLiuyan() {
    $.ajax({
        dataType: 'json',
        contentType: '/application/json;charset=UTF-8',
        async: true,
        type: "GET",
        url: 'message/showAllMessage.do',
        success: function(d) {
			// 模板导入
			$("#mainframe").load("jsp/liuyan.jsp", function(){
				// callback
				var allmess = d.allMess;
				for(var i = 0;i< allmess.length;i++){
					var mess = allmess[i];
					var data = "<li>"+mess.username+"-----"+mess.roleid+"-----:"+mess.message+"<br>"+mess.time+"</li>";
					$("#liuyan").append(data);
				}
			});
        },
        error: function() {
            alert("未知错误？");
        }
    });
}

/**
 * 保存留言   ok
 */
function saveMessage(userid) {
    var message = document.getElementById("message").value;
    $.ajax({
        dataType: 'json',
        contentType: 'application/json',
        async: true,
        type: "POST",
        data: JSON.stringify({"userid":parseInt(userid), "message": message}),
        url: 'message/saveMessToDB.do',
        success: function(d) {
			console.log(d.msg);
        },
        error: function() {
            alert("未知错误？");
        }
    });
}


/**
 * 转到下发任务   ok
 */
function toAddTask() {
    $("#mainframe").load("jsp/addTask.jsp");
     
}

/**
 * 下发任务    后端没写完
 */
function AddTask() {
    // var taskid = document.getElementById("taskid").value;
    var context = document.getElementById("context").value;
    var userid = document.getElementById("userid").value;
    var begindate = document.getElementById("begindate").value;
    var enddate = document.getElementById("enddate").value;
    
    var mydata = JSON.stringify({"context": context, "userid":userid, "begindate":begindate,"enddate":enddate});
    $.ajax({
        dataType: 'json',
        contentType: 'application/json',
        async: true,
        type: "POST",
        data: mydata,
        url: 'task/addTask.do',
        success: function(d) {
			console.log(d.msg);
        },
        error: function() {
            alert("未知错误？");
        }
    });
}

/**
 * 查看任务
 */
function showTask(userid) {
    $.ajax({
        type: "POST",
        url: 'http://localhost:8080/management/MessageAndTaskServlet',
        data: 'action=4&rad=' + Math.random() + '&userid=' + userid,
        success: function(msg) {
            $("#mainframe").html(msg);
        }
    });
}

/**
 * 转到项目的添加员工
 */
function toaddUserToPro(proid, p) {
    $.ajax({
        type: "GET",
        url: 'http://localhost:8080/management/ProjectServlet',
        data: 'action=9&rad=' + Math.random() + '&proid=' + proid + '&p=' + p,
        success: function(msg) {
            $("#mainframe").html(msg);
        }
    });
}

/**
 * 项目的添加员工
 */
function addUserToPro(userid, proid) {
    $.ajax({
        type: "GET",
        url: 'http://localhost:8080/management/ProjectServlet',
        data: 'action=10&rad=' + Math.random() + '&userid=' + userid +
            '&proid=' + proid,
        success: function(msg) {
            $("#updateresult8").html(msg);
        }
    });
}